
Imports Cdm
Imports Cdm.AppServices
Imports Cdm.AppServices.Log
Imports Cdm.DataCore.LookupTables

Partial Class Admin_appSourceOfEnrollment
    Inherits System.Web.UI.Page

#Region "CONSTANTS"
    Private Const _CLASS_NAME As String = "Admin_appSourceOfEnrollment"
#End Region

#Region "EVENT HANDLERS"

   

    Protected Sub SourceOfEnrollmentAddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SourceOfEnrollmentAddButton.Click

        Try

            Me.SourceOfEnrollmentsScrollingGridView.Visible = False

            Me.SourceOfEnrollmentAddButton.Visible = False
            'Me.GridViewSpan.Style("visibility") = "hidden"()

            Me.SubmitButton.Visible = True
            Me.CancelButton.Visible = True
            Me.SourceOfEnrollmentsDetailsView.Visible = True

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in SourceOfEnrollmentAddButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

        Try

            Dim fieldName As TextBox = CType(Me.SourceOfEnrollmentsDetailsView.FindControl("SourceOfEnrollmentDetailsTextBox"), TextBox)
            Dim fieldMaximumAge As TextBox = CType(Me.SourceOfEnrollmentsDetailsView.FindControl("MaximumAgeDetailsTextBox"), TextBox)
            Dim fieldNumberEnrollees As TextBox = CType(Me.SourceOfEnrollmentsDetailsView.FindControl("NumberOfEnrolleesDetailsTextBox"), TextBox)

            Dim sName As String = fieldName.Text.Trim()
            Dim iAge As Integer
            Dim iNumberEnrollees As Integer

            'VALIDATE DATA
            If sName = "" Then
                Me.ErrorSummary.SetSingleError("You need to enter a value for Source Of Enrollment before continuing.")
                Me.ErrorSummary.Visible = True
                ShowStatus("You need to enter a value for Source Of Enrollment before continuing.")
                Exit Sub
            End If

            'VALIDATE THE AGE
            If fieldMaximumAge.Text.Trim().Length <= 0 Then
                Me.ErrorSummary.SetSingleError("You need to enter a value for Maximum Age before continuing.")
                ShowStatus("You need to enter a value for Maximum Age before continuing.")
                Me.ErrorSummary.Visible = True
                Exit Sub
            Else
                Try
                    iAge = CType(fieldMaximumAge.Text.Trim(), Integer)
                Catch ex As Exception
                    Me.ErrorSummary.SetSingleError("Please enter a valid value for Maximum Age before continuing.")
                    Me.ErrorSummary.Visible = True
                    ShowStatus("Please enter a valid value for Maximum Age before continuing.")
                    Exit Sub
                End Try
            End If

            'VALIDATE THE NUMBER OF ENROLLEES
            If fieldNumberEnrollees.Text.Trim().Length <= 0 Then
                Me.ErrorSummary.SetSingleError("You need to enter a value for Number of Enrollees before continuing.")
                ShowStatus("You need to enter a value for Number of Enrollees before continuing.")
                Me.ErrorSummary.Visible = True
                Exit Sub
            Else
                Try
                    iNumberEnrollees = CType(fieldNumberEnrollees.Text.Trim(), Integer)
                Catch ex As Exception
                    Me.ErrorSummary.SetSingleError("Please enter a valid value for Number of Enrollees before continuing.")
                    Me.ErrorSummary.Visible = True
                    ShowStatus("Please enter a valid value for Number of Enrollees before continuing.")
                    Exit Sub
                End Try
            End If

            'CHECK IF THE NAME ALREADY EXISTS IN THE DATABASE
            If LuSourceOfEnrollment.Exists(0, sName) Then
                Me.ErrorSummary.SetSingleError("A Source Of Enrollment by that name already exists. Please enter a different name.")
                ShowStatus("A Source Of Enrollment by that name already exists. Please enter a different name.")
                Me.ErrorSummary.Visible = True
                Exit Sub
            End If

            'HIDE THE INPUT FORM
            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False
            Me.SourceOfEnrollmentsDetailsView.Visible = False

            'ADD THE NEW AFFIRAMTION
            Dim oSourceOfEnrollment As New LuSourceOfEnrollment()
            oSourceOfEnrollment.Description = sName
            oSourceOfEnrollment.MaximumAge = iAge
            oSourceOfEnrollment.NumberOfEnrollees = iNumberEnrollees
            oSourceOfEnrollment.Persist()
            ShowStatus("Source of Enrollment Added - " + sName)

            'RETRIEVE A NEW DATASET
            Me.SourceOfEnrollmentsObjectDataSource.Select()
            Me.SourceOfEnrollmentsScrollingGridView.DataBind()

            'CLEAR THE INPUT FORM
            fieldName.Text = String.Empty
            fieldMaximumAge.Text = String.Empty

            'CLEAR THE ERROR MESSAGE
            If Me.ErrorSummary.Visible Then
                Me.ErrorSummary.Visible = False
            End If

            'DISPLAY THE GRIDVIEW
            Me.SourceOfEnrollmentsScrollingGridView.Visible = True
            Me.SourceOfEnrollmentAddButton.Visible = True
            'Me.GridViewSpan.Style("visibility") = "visible"


        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in SourceOfEnrollmentAddButton_Click() method.", ex)
            ClearStatus()
        End Try

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CancelButton.Click

        Try

            Dim fieldName As TextBox = CType(Me.SourceOfEnrollmentsDetailsView.FindControl("SourceOfEnrollmentDetailsTextBox"), TextBox)
            Dim fieldMaximumAge As TextBox = CType(Me.SourceOfEnrollmentsDetailsView.FindControl("MaximumAgeDetailsTextBox"), TextBox)

            fieldName.Text = String.Empty
            fieldMaximumAge.Text = String.Empty

            Me.SubmitButton.Visible = False
            Me.CancelButton.Visible = False
            Me.SourceOfEnrollmentsDetailsView.Visible = False
            Me.ErrorSummary.Visible = False


            SourceOfEnrollmentsScrollingGridView.Visible = True
            Me.SourceOfEnrollmentAddButton.Visible = True
            'Me.GridViewSpan.Style("visibility") = "visible"
            ShowStatus("Add canceled.")
        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in CancelButton_Click() method.", ex)
            ClearStatus()
        End Try

    End Sub

    Protected Sub SourceOfEnrollmentsDetailsView_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles SourceOfEnrollmentsDetailsView.ItemInserting

        Try

            Me.SourceOfEnrollmentsDetailsView.Visible = False

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in SourceOfEnrollmentsDetailsView_ItemInserting() method.", ex)
        End Try

    End Sub

#End Region

    Protected Sub SourceOfEnrollmentsScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles SourceOfEnrollmentsScrollingGridView.RowCancelingEdit
        Try
            If Me.ErrorSummary.Visible = True Then
                Me.ErrorSummary.Visible = False
                Me.SourceOfEnrollmentsScrollingGridView.Visible = True
                'Me.GridViewSpan.Style("visibility") = "visible"
                ShowStatus("Edit cancelled.")
            End If

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in SourceOfEnrollmentsScrollingGridView_RowCancelingEdit() method.", ex)
        End Try
    End Sub

    Protected Sub SourceOfEnrollmentsScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SourceOfEnrollmentsScrollingGridView.RowDataBound

        Try
            If e.Row.DataItemIndex = 0 Then
                AccessibilityController.AddImageToSortableGridHeaders(SourceOfEnrollmentsScrollingGridView)
            End If
            Dim fieldName As TextBox = CType(e.Row.FindControl("SourceOfEnrollmentDetailsTextBox"), TextBox)
            If Not fieldName Is Nothing Then
                SetFocus(fieldName.ClientID)
            End If

        Catch ex As Exception
            ErrorLog(_CLASS_NAME, "An error occurred in SourceOfEnrollmentsScrollingGridView_RowDataBound() method.", ex)
        End Try

    End Sub

    Protected Sub SourceOfEnrollmentsScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles SourceOfEnrollmentsScrollingGridView.RowUpdating
        Dim strNV As String
        Dim sValue As String
        Dim iIdx As Integer
        Dim strOV As String
        Dim iSourceOfEnrollmentId As Integer = CType(e.Keys(0), Integer)
        Dim sMaxAge As String
        Dim iAge As Integer
        Dim sNumberEnrollees As String
        Dim iNumberEnrollees As Integer
        Try

            If e.OldValues.Item(iIdx) IsNot Nothing Then
                strOV = e.OldValues.Item(iIdx).ToString.ToUpper
            Else
                strOV = ""
            End If

            If e.NewValues.Item(iIdx) IsNot Nothing Then
                strNV = e.NewValues.Item(iIdx).ToString.ToUpper
                If strOV = strNV Then
                    If Me.ErrorSummary.Visible = True Then
                        Me.ErrorSummary.Visible = False
                    End If
                End If

                'CHECK IF AN SourceOfEnrollment RECORD BY THIS NAME ALREADY EXISTS
                If LuSourceOfEnrollment.Exists(iSourceOfEnrollmentId, strNV) Then
                    sValue = "An Source Of Enrollment by that name already exists. Please enter a different name."
                    Me.ErrorSummary.SetSingleError(sValue)
                    Me.ErrorSummary.Visible = True
                    ShowStatus(sValue)
                    e.Cancel = True
                    Exit Sub
                End If

                sMaxAge = e.NewValues.Item(1).ToString().Trim()
                If sMaxAge.Length <= 0 Then
                    Me.ErrorSummary.SetSingleError("You need to enter a value for Maximum Age before continuing.")
                    Me.ErrorSummary.Visible = True
                    ShowStatus("You need to enter a value for Maximum Age before continuing.")
                    e.Cancel = True
                    Exit Sub
                Else
                    Try
                        iAge = CType(sMaxAge, Integer)
                    Catch ex As Exception
                        Me.ErrorSummary.SetSingleError("Please enter a valid value for Maximum Age before continuing.")
                        Me.ErrorSummary.Visible = True
                        ShowStatus("Please enter a valid value for Maximum Age before continuing.")
                        e.Cancel = True
                        Exit Sub
                    End Try
                End If

                sNumberEnrollees = e.NewValues.Item(2).ToString().Trim()

                If sNumberEnrollees.Length <= 0 Then
                    Me.ErrorSummary.SetSingleError("You need to enter a value for Number of Enrollees before continuing.")
                    Me.ErrorSummary.Visible = True
                    ShowStatus("You need to enter a value for Number of Enrollees before continuing.")
                    e.Cancel = True
                    Exit Sub
                Else
                    Try
                        iNumberEnrollees = CType(sNumberEnrollees, Integer)
                    Catch ex As Exception
                        Me.ErrorSummary.SetSingleError("Please enter a valid value for Number of Enrollees before continuing.")
                        Me.ErrorSummary.Visible = True
                        ShowStatus("Please enter a valid value for Number of Enrollees before continuing.")
                        e.Cancel = True
                        Exit Sub
                    End Try
                End If

                'Passed error checking now update database
                Me.ErrorSummary.Visible = False
                ShowStatus("Edit Successful for Source of Enrollment " + strNV)
            Else
                sValue = "You must enter a value in the field before updating."
                Me.ErrorSummary.SetSingleError(sValue)
                Me.ErrorSummary.Visible = True
                ShowStatus(sValue)
                e.Cancel = True
            End If

        Catch ex As Exception
            ClearStatus()
            ErrorLog(_CLASS_NAME, "An error occurred in SourceOfEnrollmentsScrsollingGridView_RowUpdating() method.", ex)
        End Try

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ClearStatus()
        EntityController.JavaScriptAddChildDiv(Me.SourceOfEnrollmentsScrollingGridView.DivClientId, CInt(Me.SourceOfEnrollmentsScrollingGridView.ToolTip))
        'Me.RegisterStartupScript("scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.SourceOfEnrollmentsScrollingGridView))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.SourceOfEnrollmentsScrollingGridView), True)
        ScriptManager.RegisterClientScriptBlock(Me.SourceOfEnrollmentsScrollingGridView, Me.SourceOfEnrollmentsScrollingGridView.GetType(), _
                "scrollDivAsync", EntityController.JavaScriptScrollingGridStartup(Me.SourceOfEnrollmentsScrollingGridView), True)
    End Sub

    Private Sub ShowStatus(ByVal message As String)
        Me.SourceOfEnrollmentsScrollingGridView.StatusText = message
    End Sub

    Private Sub ClearStatus()
        Me.SourceOfEnrollmentsScrollingGridView.StatusText = ""
    End Sub
End Class
